package com.bluemsun.naturallanguageprocessing.util;

import com.bluemsun.naturallanguageprocessing.entity.User;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.exception.UserNullException;
import com.mysql.cj.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
@Component
public class ConductRedisUtil {
    private  static Logger logger = LoggerFactory.getLogger(ConductRedisUtil.class);

    @Autowired
    RedisUtil redisUtil;

    /**
     * token校验
     * @describe :如果token不为空，返回true,否则为false
     * @param request
     * @return
     */
    public boolean checkToken(HttpServletRequest request){
        return !StringUtils.isNullOrEmpty(request.getHeader("token"));
    }

    /**
     * 获取user对象
     * @param token
     * @return
     */
    /**
     * @author     ：hj
     * @date       ：update in 2021/2/14 11:03
     * @description：对象应该为userAPi
     * @modified By：
     * @version:
     */
    public UserApi getUserFromRedis(String token){
        String jsonUser = redisUtil.get(token);
        if (StringUtils.isNullOrEmpty(jsonUser)){
            logger.error("redis中不存在该token所对应的user对象,token无效或者过期");
            throw new UserNullException("redis中不存在该token所对应的user对象,token无效或者过期");
        }
        return JsonUtil.string2Obj(jsonUser, UserApi.class);
    }

//    public  String readHeaderToken(HttpServletRequest request){
//        String token = request.getHeader("token");
//        logger.info("拿到的token{}",token);
//        if(token != null){
//            String token1 = redisUtil.get(token);
//            logger.info("得到的登录用户查询值{}",token1);
//            if(org.apache.commons.lang3.StringUtils.isBlank(token)&&org.apache.commons.lang3.StringUtils.isBlank(token1)){
//                return "token无效或者过期";
//            }
//            else{
//                return token1;
//            }
//        }
//        return null;
//    }
    public  String readHeaderCode(HttpServletRequest request){
        String Code = request.getHeader("Code");
        logger.info("拿到的Code{}",Code);
        if(Code != null){
            String code1 = redisUtil.get(Code);
            if(org.apache.commons.lang3.StringUtils.isBlank(code1)){
                return "code无效或者过期";
            }
            else{
                return code1;
            }
        }
        return "code无效或者过期";
    }
    public  void delLoginToken(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        try {
            if(token != null){
                logger.info("清除token{}",token);
                redisUtil.del(token);
            }
        } catch (Exception exception) {
            logger.error("清除token出错{}",exception);
        }
    }
    public void delCodeToken(HttpServletRequest request, HttpServletResponse response){
        String Code = request.getHeader("Code");
        try {
            if(Code != null){
                logger.info("清除Code{}",Code);
                redisUtil.del(Code);
            }
        } catch (Exception exception) {
            logger.error("清除Code出错{}",exception);
        }
    }

    public  void removeFromCache(String keyPrefix) {
        Set<String> keySet = redisUtil.keys(keyPrefix + "*");
        for (String key : keySet) {
            redisUtil.del(key);
        }
    }

}
